package com.egg.ylt.presenter.impl;

import com.egg.ylt.Utils.API;
import com.egg.ylt.Utils.Constants;
import com.egg.ylt.pojo.SwimHistoryEntity;
import com.egg.ylt.pojo.SwimHistoryListEntity;
import com.egg.ylt.presenter.ISwimHistoryPresenter;
import com.egg.ylt.view.ISwimHistoryView;
import com.yonyou.framework.library.bean.ErrorBean;
import com.yonyou.framework.library.common.CommonUtils;
import com.yonyou.framework.library.mvp.impl.BasePresenter;
import com.yonyou.framework.library.net.ReqCallBack;
import com.yonyou.framework.library.net.RequestManager;

import java.util.HashMap;

/**
 * author:        yupeng
 * created on:    2019/2/25 18:16
 * description:   游泳纪录
 */
public class SwimHistoryPresenterImpl extends BasePresenter<ISwimHistoryView> implements ISwimHistoryPresenter {
    private int pageNum = 1;
    private String pageSize = "10";

    public void onInit() {
        onRefresh();
    }

    public void onRefresh() {
        pageNum = 1;
        getSwimHistory(Constants.BABYID);
        getSwimHistoryList(Constants.BABYID, pageNum+"", pageSize);
    }

    public void onLoadMore() {
        getSwimHistoryList(Constants.BABYID, ++pageNum+"", pageSize);
    }

    @Override
    public void getSwimHistory(String babyId) {
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("babyId", babyId);
        RequestManager.getInstance().requestGetByAsyn(API.GET_BABY_LIFE_EQUIPMENT_DATA, hashMap, new ReqCallBack<SwimHistoryEntity>() {
            @Override
            public void onReqSuccess(SwimHistoryEntity result) {
                mView.showSwimHistory(result);
            }

            @Override
            public void onFailure(String result) {
            }

            @Override
            public void onReqFailed(ErrorBean error) {
            }
        });
    }

    @Override
    public void getSwimHistoryList(String babyId, final String pageNum, final String pageSize) {
        mView.showDialogLoading("数据加载中");
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("babyId", babyId);
        hashMap.put("pageNum", pageNum);
        hashMap.put("pageSize", pageSize);
        RequestManager.getInstance().requestGetByAsyn(API.GET_BABY_LIFE_EQUIPMENT_DATA_PAGE, hashMap, new ReqCallBack<SwimHistoryListEntity>() {
            @Override
            public void onReqSuccess(SwimHistoryListEntity result) {
                mView.dismissDialogLoading();
                mView.showSwimHistoryList(result, "1".equals(pageNum));
                mView.setListViewToLastPageAndNoData(Integer.parseInt(pageNum)*Integer.parseInt(pageSize) >= Integer.parseInt(result.getTotalPage()), result.getList().isEmpty());
                mView.stopListViewRefreshAndLoadMore();
            }

            @Override
            public void onFailure(String result) {
                mView.dismissDialogLoading();
                CommonUtils.makeEventToast(mContext, result, false);
            }

            @Override
            public void onReqFailed(ErrorBean error) {
                mView.dismissDialogLoading();
                CommonUtils.makeEventToast(mContext, error.getMsg(), false);
            }
        });
    }
}
